package edu.uwlax.cs.oayonlinestore.client.gui.shop;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.Order;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.Order.OrderChangeListener;
import edu.uwlax.cs.oayonlinestore.client.gui.shared.ProductTree;
import edu.uwlax.cs.oayonlinestore.client.utils.Currency;
import edu.uwlax.cs.oayonlinestore.client.utils.StackPage;
import edu.uwlax.cs.oayonlinestore.client.utils.StackPanel;
import edu.uwlax.cs.oayonlinestore.client.utils.VSpacer;

public class PageShop extends StackPage {

	private class MiniShoppingCart extends VerticalPanel implements
			OrderChangeListener {

		private HTML status = new HTML();

		private HTML user = new HTML();

		public MiniShoppingCart() {
			super();
			Hyperlink cart = new Hyperlink("<b>Shopping Cart</b>", true, frame
					.getCart().getAnchor());
			add(user);
			add(cart);
			add(status);
			setStyleName("MiniShoppingCart");
			Order.get().addChangeListener(this);
			orderChanged(Order.get());
		}

		public void orderChanged(Order order) {
			String count = String.valueOf(order.getItemCount());
			String total = Currency.format(order.getTotal());
			status.setHTML(count + " Items<br>" + total);
			if (order.getUser().getId() != User.get().getId())
				user.setHTML(order.getUser().getFirstname() + " "
						+ order.getUser().getLastname() + "'s");
			else
				user.setHTML("");

		}

	}

	public class ShopPanel extends StackPanel {

		private PageShoppingCart cart = new PageShoppingCart();

		private PageCategory category = new PageCategory();

		private PageConfirm confirm = new PageConfirm();

		private PageDeliver deliver = new PageDeliver();

		private PageGiftCards giftCards = new PageGiftCards();

		private PageMain main = new PageMain();

		private PageReciept reciept = new PageReciept();

		private PageSubCategory subcategory = new PageSubCategory();

		public ShopPanel(StackPage parent) {
			super(parent);
			add(main);
			add(confirm);
			add(deliver);
			add(cart);
			add(category);
			add(subcategory);
			add(giftCards);
			add(reciept);
		}

		public PageShoppingCart getCart() {
			return cart;
		}

		public PageCategory getCategory() {
			return category;
		}

		public PageConfirm getConfirm() {
			return confirm;
		}

		public PageDeliver getDeliver() {
			return deliver;
		}

		public PageGiftCards getGiftCards() {
			return giftCards;
		}

		public PageMain getMain() {
			return main;
		}

		public PageReciept getReciept() {
			return reciept;
		}

		public PageSubCategory getSubcategory() {
			return subcategory;
		}

	}

	private ShopPanel frame = new ShopPanel(this);

	protected void create() {
		HorizontalPanel p = new HorizontalPanel();
		frame.setWidth("100%");
		frame.init();
		p.add(createLeft());
		p.add(new VSpacer("32px"));
		p.add(frame);
		setWidget(p);
	}

	private Widget createLeft() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(new MiniShoppingCart());
		Hyperlink products = new Hyperlink("<br><b>Products</b>", true, frame
				.getMain().getAnchor());
		panel.add(products);
		panel.add(new ProductTree(frame.category, frame.subcategory));
		return panel;
	}

	public ShopPanel getFrame() {
		return frame;
	}

	public String getID() {
		return "Shop";
	}

	protected boolean onShow(String param) {
		frame.onHistoryChanged(param);
		return true;
	}

}
